package com.kupurui.asteacher.ui.mine;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.frame.util.AppJsonUtil;
import com.kupurui.asteacher.R;
import com.kupurui.asteacher.bean.UpdateInfo;
import com.kupurui.asteacher.http.Banben;
import com.kupurui.asteacher.ui.BaseAty;
import com.kupurui.asteacher.ui.MainActivity;
import com.kupurui.asteacher.update.DownloaderUtil;
import com.kupurui.asteacher.update.UpdateManager;
import com.kupurui.asteacher.utils.UserManger;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 关于我们
 * Created by Administrator on 2017/7/18.
 */

public class AboutAty extends BaseAty {

    @Bind(R.id.tv_bar_title)
    TextView tvBarTitle;
    @Bind(R.id.m_toolbar)
    Toolbar mToolbar;
    @Bind(R.id.tv_version)
    TextView tvVersion;
    @Bind(R.id.tv_version_update)
    TextView tvVersionUpdate;
    @Bind(R.id.ll_version_update)
    LinearLayout llVersionUpdate;

    UpdateInfo updateInfo;

    @Override
    public int getLayoutId() {
        return R.layout.about_aty;
    }

    @Override
    public void initData() {
        initToolbar(mToolbar,"关于我们");
        tvVersion.setText("当前版本V"+ DownloaderUtil.getVersionName(this));
    }

    @Override
    public void requestData() {
        showLoadingContent();
        new Banben().index(DownloaderUtil.getVersionCode(this)+"",this,0);
    }

    @OnClick({R.id.rl_agreement,R.id.ll_version_update})
    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){
            case R.id.rl_agreement:
                startActivity(AgreementAty.class,null);
                break;
            case R.id.ll_version_update://版本更新
                if (updateInfo.getStatus().equals("0")){
                    showToast("当前已是最新版本");
                }else{
                    final String versionCode = updateInfo.getVersion();
                    if (UserManger.getIgnoreVersion().equals(versionCode)) {
                        return;
                    }
                    if (updateInfo.getIs_must().equals("0")) {
                        new AlertDialog.Builder(this).setTitle("有新的版本").setMessage(Html.fromHtml(updateInfo.getContent())).setNegativeButton("以后再说", null).setPositiveButton("更新", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                UpdateManager updateManager = new UpdateManager(AboutAty.this,updateInfo.getAppurl());
                                updateManager.showDownloadDialog();
                            }
                        }).setNeutralButton("忽略该版本", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                UserManger.setIgnoreVersion(versionCode);
                            }
                        }).create().show();
                    } else {
                        new AlertDialog.Builder(this).setTitle("有新的版本").setMessage(Html.fromHtml(updateInfo.getContent())).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                finish();
                            }
                        }).setPositiveButton("更新", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                UpdateManager updateManager = new UpdateManager(AboutAty.this,updateInfo.getAppurl());
                                updateManager.showDownloadDialog();
                            }
                        }).setCancelable(false).show();
                    }
                }
                break;
        }
    }

    @Override
    public void onSuccess(String result, Call call, Response response, int what) {
        switch (what){
            case 0:
                updateInfo = AppJsonUtil.getObject(result,UpdateInfo.class);
                if (updateInfo.getStatus().equals("1")){
                    tvVersionUpdate.setText("有新版本");
                }else {
                    tvVersionUpdate.setText("当前已是最新版本");
                }
                break;
        }
        super.onSuccess(result, call, response, what);
    }
}
